﻿using Microsoft.Win32;
using PLC.Interface.SysLogs;
using PLCWpfTest.Common;
using PLCWpfTest.Common.Extensions;
using PLCWpfTest.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using YWM.DoubleCommom;
using YWM.Dto;

namespace PLCWpfTest.ViewModels
{
    public class AlarmTracingViewModel : ManagementViewModel
    {
        private readonly ISysLogAppService _sysLogAppService;

        public RelayCommand ExportExcelCommand { get; set; }

        public NameValueModel<byte>[] AlarmTypes { get; set; }

        public SysLogResultRequestDto Query { get; set; }

        private PagedResultDto<SysLogDto> _sysLogPaged = new PagedResultDto<SysLogDto>();
        public PagedResultDto<SysLogDto> SysLogPaged
        {
            get { return _sysLogPaged; }
            set { SetProperty(ref _sysLogPaged, value); }
        }

        public AlarmTracingViewModel(ISysLogAppService sysLogAppService, RelayCommand exportExcelCommand)
        {
            AlarmTypes = new NameValueModel<byte>[] {
                new NameValueModel<byte>(Lang["消除"],1),
                new NameValueModel<byte>(Lang["触发"],0)
            };
            _sysLogAppService = sysLogAppService;
            ExportExcelCommand = exportExcelCommand;
            ExportExcelCommand.BeforeCanExecute += ExportExcelCommand_BeforeCanExecute;
            ExportExcelCommand.Executing += ExportExcelCommand_Executing;
            Query = new SysLogResultRequestDto();
            //Query = new SysLogResultRequestDto { StartTime = DateTime.Today, EndTime = DateTime.Today.AddDays(1).AddSeconds(-1) };
        }

        protected override bool LoadCommand_BeforeExecute(object parameter)
        {
            if (Query.StartTime != null && Query.EndTime != null && DateTime.Compare(Query.StartTime.Value, Query.EndTime.Value) >= 0)
            {
                this.ShowWarning("结束时间不能小于开始时间");
                return false;
            }
            return true;
        }

        protected override async Task LoadCommand_Executing(object parameter)
        {
            if (parameter != null)
            {
                Current = 1;
            }
            Query.SkipCount = (Current - 1) * Query.MaxResultCount;
            SysLogPaged = await _sysLogAppService.GetAll(Query);
        }

        private bool ExportExcelCommand_BeforeCanExecute(object arg)
        {
            return SysLogPaged.TotalCount > 0;
        }

        private void ExportExcelCommand_Executing(object obj)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.Title = Lang["导出Excel"];
            saveFileDialog.FileName = $"{Lang["报警追溯"]}{DateTime.Now.ToString("yyyyMMddHHmmss")}";
            saveFileDialog.Filter = "Excel files (*.xlsx)|*.xlsx";
            if (saveFileDialog.ShowDialog() == true)
            {
                ExcelUtil.Export((System.Collections.ICollection)SysLogPaged.Items, saveFileDialog.FileName, new Dictionary<string, string>
                {
                    {"VarName",Lang["变量名"]},
                    {"AlarmType",Lang["报警类型"]},
                    {"OperatorName",Lang["操作员"]},
                    {"OperationTime",Lang["操作时间"]},
                    {"Description",Lang["报警信息"]}
                });
            }
        }
    }
}